import {
  reqShopCartList,
  reqAddOrUpdateShopCart,
  reqUpdateOneCartIsCheck,
  reqUpdateAllCartIsCheck,
  reqDeleteAllCart,
  reqGoodsDetailInfo

} from "@/api"

const state = {
  shopCartList: []
}
const mutations = {
  RECEIVE_SHOPCARTLIST(state, shopCartList) {
    state.shopCartList = shopCartList
  }
}
const actions = {

  // 请求获取购物车列表
  async getShopCartList({commit}) {
    const result = await reqShopCartList()
    if (result.code === 200) {
      commit('RECEIVE_SHOPCARTLIST', result.data)
    
    }
  },
  // 请求加减
  async addOrUpdateShopCart({commit}, {
    skuId,
    skuNum
  }) {
    const result = await reqAddOrUpdateShopCart(skuId, skuNum)
    if (result.code === 200) {
      return 'ok'
      // 返回200代表添加购物车成功了
    } else {
      // 代表添加购物车失败了
      return Promise.reject(new Error('failed'))
    }
  },
  // 请求修改购物车单个的选中状态
  async updateOneCartIsCheck({commit}, {
    skuId,
    isChecked
  }) {
    const result = await reqUpdateOneCartIsCheck(skuId, isChecked)
    if (result.code === 200) {
      return 'ok'
    } else {
      return Promise.reject(new Error('failed'))
    }
  },
  
   // 请求修改购物车多个选中状态
  async updateAllCartIsCheck({commit},{skuIdList,isChecked}){
    const result = await reqUpdateAllCartIsCheck(skuIdList,isChecked)
    if(result.code === 200){
      return 'ok'
    }else{
      return Promise.reject(new Error('failed'))
    } 
  },

  //删除购物车列表
 // 请求删除多个购物车
  async deleteAllCart({
    commit
  }, skuIdList) {
    const result = await reqDeleteAllCart(skuIdList)
    if (result.code === 200) {
      return 'ok'
    } else {
      return Promise.reject(new Error('failed'))
    }
  }

  








  


}
const getters = {}
export default {
  state,
  mutations,
  actions,
  getters
}